$(function(){$.widget("ui.dialog",$.ui.dialog,{options:{clickOutside:false,clickOutsideTrigger:""},open:function(){var e=$(this.options.clickOutsideTrigger);var t=this;if(this.options.clickOutside){$(document).on("click.ui.dialogClickOutside"+t.eventNamespace,function(n){if($(n.target).css("display")!="none"&&$(n.target).attr("from")!='simpledialog'&&$(n.target).closest($(e)).length==0&&$(n.target).closest($(t.uiDialog)).length==0){t.close()}})}this._super()},close:function(){var e=this;$(document).off("click.ui.dialogClickOutside"+e.eventNamespace);this._super()}})})
function cursorDialogResize(dialog,size){
	var d = dialog.parents(".ui-dialog:first");
	if(size.width!=null){
		var w = d.width();
		var l = d.offset().left;
		d.css("width",size.width);
		d.css("left",l-size.width+w);
	}
	if(size.height!=null){
		var h = d.height();
		var t = d.offset().top;
		d.find(".ui-newdialog-content").css("height",size.height);
		d.css("top",t-size.height+h);
	}
	if(d.find("[class=dialogFromRight]").length>0)d.find("[class=dialogFromRight]").css("left",d.width());
	if(d.find("[class=dialogFromBottom]").length>0)d.find("[class=dialogFromBottom]").css("top",d.height());
}
function dynamicLoadingCursorDialog(position,caller,dynamicDom,title,file,buttons,loadFilefinish,args){
	var dialog = null;
	var obj = {
		position:position,caller:caller,
		title:title,dialog:dialog,buttons:buttons,
		args:args,loadFilefinish:loadFilefinish	
	};
	loadDynamicJSP(dynamicDom,file,function(obj){
		obj.dialog = cursorDialog(obj.position,obj.caller,obj.title,"<div id='loadingScreen'></div>",null,obj.args);
	},function(resp,obj){
		if(obj.dialog!=null){
			obj.dialog.find(".ui-newdialog-content").html(resp);
			bindButtonDialog(obj.dialog,obj.buttons,obj.args);
		}
		else
			obj.dialog = cursorDialog(obj.position,obj.caller,obj.title,resp,obj.buttons,obj.args);
		if(obj.loadFilefinish!=null) obj.loadFilefinish(obj.dialog,obj.args);
	},obj);
}
function cursorDialog(position,caller,title,content,buttons,args){
	var dialog = clickOutSideDialog(caller,title,content,buttons,args);
	var left = 0;
	var top = 0;
	var pointer = "";
	var d = dialog.parents(".ui-dialog:first");
	if(position == "right"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - (dialog.height()/2));
			d.css("left",caller.offset().left - dialog.width() - 16);
		}
		pointer = $("<div class='dialogFromRight'><div>");
		left = d.width();
		top = (d.height()/2) - 8;
	}
	else if(position == "right,top"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - 31);
			d.css("left",caller.offset().left - dialog.width() - 16);
		}
		pointer = $("<div class='dialogFromRight'><div>");
		left = d.width();
		top = 20;
	}
	else if(position == "right,bottom"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - dialog.height() + 50);
			d.css("left",caller.offset().left - dialog.width() - 16);
		}
		pointer = $("<div class='dialogFromRight'><div>");
		left = d.width();
		top = d.height() - 60;
	}
	else if(position == "left"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - (dialog.height()/2));
			d.css("left",caller.offset().left + caller.width() + 17);
		}
		pointer = $("<div class='dialogFromLeft'><div>");
		left = -16;
		top = (d.height()/2) - 8;
	}
	else if(position == "left,top"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - 31);
			d.css("left",caller.offset().left + caller.width() + 17);
		}
		pointer = $("<div class='dialogFromLeft'><div>");
		left = -16;
		top = 20;
	}
	else if(position == "left,bottom"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - dialog.height() + 50);
			d.css("left",caller.offset().left + caller.width() + 17);
		}
		pointer = $("<div class='dialogFromLeft'><div>");
		left = -16;
		top = d.height() - 60;
	}
	else if(position == "top"){
		if(caller!=null){
			d.css("top",caller.offset().top + caller.height() + 16);
			d.css("left",caller.offset().left + (caller.width()/2) - (d.width()/2) + 10);
		}
		pointer = $("<div class='dialogFromTop'><div>");
		left = (d.width()/2) - 10;
		top = -16;
	}
	else if(position == "top,left"){
		if(caller!=null){
			d.css("top",caller.offset().top + caller.height() + 16);
			d.css("left",caller.offset().left + (caller.width()/2) - 20);
		}
		pointer = $("<div class='dialogFromTop'><div>");
		left = 20;
		top = -16;
	}
	else if(position == "top,right"){
		if(caller!=null){
			d.css("top",caller.offset().top + caller.height() + 16);
			d.css("left",caller.offset().left + (caller.width()/2) - d.width() + 40);
		}
		pointer = $("<div class='dialogFromTop'><div>");
		left = d.width() - 40;
		top = -16;
	}
	else if(position == "bottom"){
		if(caller!=null){
			d.css("top",caller.offset().top - d.height() - 16);
			d.css("left",caller.offset().left + (caller.width()/2) - (d.width()/2) + 10);
		}
		pointer = $("<div class='dialogFromBottom'><div>");
		left = (d.width()/2) - 10;
		top = d.height();
	}
	else if(position == "bottom,left"){
		if(caller!=null){
			d.css("top",caller.offset().top - d.height() - 16);
			d.css("left",caller.offset().left + (caller.width()/2) - 20);
		}
		pointer = $("<div class='dialogFromBottom'><div>");
		left = 20;
		top = d.height();
	}
	else if(position == "bottom,right"){
		if(caller!=null){
			d.css("top",caller.offset().top - d.height() - 16);
			d.css("left",caller.offset().left + (caller.width()/2) - d.width() + 40);
		}
		pointer = $("<div class='dialogFromBottom'><div>");
		left = d.width() - 40;
		top = d.height();
	}
	
	if(pointer != ""){
		d.css("overflow","visible");
		dialog.css("overflow","visible");
		dialog.append(pointer);
		pointer.css("left",left);
		pointer.css("top",top);
	}
	return dialog;
}

function cursorNewOffSetDialog(position,caller,dialog){
	var left = 0;
	var top = 0;
	var pointer = "";
	var d = dialog.parents(".ui-dialog:first");
	if(position == "right"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - (dialog.height()/2));
			d.css("left",caller.offset().left - dialog.width() - 16);
		}
		pointer = $("<div class='dialogFromRight'><div>");
		left = d.width();
		top = (d.height()/2) - 8;
	}
	else if(position == "right,top"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - 31);
			d.css("left",caller.offset().left - dialog.width() - 16);
		}
		pointer = $("<div class='dialogFromRight'><div>");
		left = d.width();
		top = 20;
	}
	else if(position == "right,bottom"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - dialog.height() + 50);
			d.css("left",caller.offset().left - dialog.width() - 16);
		}
		pointer = $("<div class='dialogFromRight'><div>");
		left = d.width();
		top = d.height() - 60;
	}
	else if(position == "left"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - (dialog.height()/2));
			d.css("left",caller.offset().left + caller.width() + 17);
		}
		pointer = $("<div class='dialogFromLeft'><div>");
		left = -16;
		top = (d.height()/2) - 8;
	}
	else if(position == "left,top"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - 31);
			d.css("left",caller.offset().left + caller.width() + 17);
		}
		pointer = $("<div class='dialogFromLeft'><div>");
		left = -16;
		top = 20;
	}
	else if(position == "left,bottom"){
		if(caller!=null){
			d.css("top",caller.offset().top + (caller.height()/2) - dialog.height() + 50);
			d.css("left",caller.offset().left + caller.width() + 17);
		}
		pointer = $("<div class='dialogFromLeft'><div>");
		left = -16;
		top = d.height() - 60;
	}
	else if(position == "top"){
		if(caller!=null){
			d.css("top",caller.offset().top + caller.height() + 16);
			d.css("left",caller.offset().left + (caller.width()/2) - (d.width()/2) + 10);
		}
		pointer = $("<div class='dialogFromTop'><div>");
		left = (d.width()/2) - 10;
		top = -16;
	}
	else if(position == "top,left"){
		if(caller!=null){
			d.css("top",caller.offset().top + caller.height() + 16);
			d.css("left",caller.offset().left + (caller.width()/2) - 20);
		}
		pointer = $("<div class='dialogFromTop'><div>");
		left = 20;
		top = -16;
	}
	else if(position == "top,right"){
		if(caller!=null){
			d.css("top",caller.offset().top + caller.height() + 16);
			d.css("left",caller.offset().left + (caller.width()/2) - d.width() + 40);
		}
		pointer = $("<div class='dialogFromTop'><div>");
		left = d.width() - 40;
		top = -16;
	}
	else if(position == "bottom"){
		if(caller!=null){
			d.css("top",caller.offset().top - d.height() - 16);
			d.css("left",caller.offset().left + (caller.width()/2) - (d.width()/2) + 10);
		}
		pointer = $("<div class='dialogFromBottom'><div>");
		left = (d.width()/2) - 10;
		top = d.height();
	}
	else if(position == "bottom,left"){
		if(caller!=null){
			d.css("top",caller.offset().top - d.height() - 16);
			d.css("left",caller.offset().left + (caller.width()/2) - 20);
		}
		pointer = $("<div class='dialogFromBottom'><div>");
		left = 20;
		top = d.height();
	}
	else if(position == "bottom,right"){
		if(caller!=null){
			d.css("top",caller.offset().top - d.height() - 16);
			d.css("left",caller.offset().left + (caller.width()/2) - d.width() + 40);
		}
		pointer = $("<div class='dialogFromBottom'><div>");
		left = d.width() - 40;
		top = d.height();
	}
	
	if(pointer != ""){
		$(".dialogFromLeft:visible").css("display","none");
		d.css("overflow","visible");
		dialog.css("overflow","visible");
		dialog.append(pointer);
		pointer.css("left",left);
		pointer.css("top",top);
	}
	return dialog;
}

function clickOutSideDialog(caller,title,content,arrayButton,args)
{
	if($("#loadingScreen").is(":visible")) closeWaitingDialog();
	var msgDialog = $("#BlankDialog").clone();
	msgDialog.dialog({
		resizable : false,
		modal: false,
		clickOutside: true,
		clickOutsideTrigger: caller,
		close: function() { msgDialog.dialog('destroy').remove(); },
	});
	var maxZ = MaxDivZIndex();
	msgDialog.parent().find('.ui-dialog-titlebar').css("display","none");
	msgDialog.parent().css("z-index",maxZ+2);
	msgDialog.parent().next().css("z-index",maxZ+1);
	
	//msgDialog.parent().find('')
	msgDialog.css("padding",0);
	msgDialog.css("margin",0);
	msgDialog.html('<div class="ui-newdialog-titlebar"><div>'+title+'<span class="colse_icon " style="float:right"></span></div></div>' +
		'<div class="ui-newdialog-content">'+content+'</div>' +
		'<div class="ui-newdialog-button"><div style="text-align:center;"></div></div>');
	msgDialog.css("min-height","")
	msgDialog.find(".colse_icon").click(function() {msgDialog.dialog("close");});
	
	if(arrayButton != null)
	for(var i=arrayButton.length-1;i>=0;i--)
	{
		var style = "float:right";
		if(arrayButton[i].style != null) style = arrayButton[i].style;
		var btn = $('<button style="'+style+'" class="btn roundAll marLR">' + arrayButton[i].title + '</button>');
		btn.bind('click', arrayButton[i], function(event) {
			event.data.callback(msgDialog,args);
		});
		msgDialog.find(".ui-newdialog-button div").append(btn);
	}
	return msgDialog;
}

function dynamicLoadingNonCursorDialog(caller,dynamicDom,title,file,buttons,loadFilefinish,args){
	var dialog = null;
	var obj = {
		caller:caller,
		title:title,
		dialog:dialog,
		buttons:buttons,
		args:args,
		loadFilefinish:loadFilefinish	
	};
	debugger;
	loadDynamicJSP(dynamicDom,file,function(obj){
		obj.dialog = clickOutSideDialog(obj.caller,obj.title,"<div id='loadingScreen'></div>",null,obj.args);
	},function(resp,obj){
		if(obj.dialog!=null){
			obj.dialog.find(".ui-newdialog-content").html(resp);
			bindButtonDialog(obj.dialog,obj.buttons,obj.args);
		}
		else
			obj.dialog = clickOutSideDialog(obj.caller,obj.title,resp,obj.buttons,obj.args);
		if(obj.loadFilefinish!=null) obj.loadFilefinish(obj.dialog,obj.args);
	},obj);
}